<?php
class AgentpriceModel extends Model {
	//
	public $discount;
	//获取一个商品的出售价格
	public function getagentpricebylevel($siteid, $num){
		$num = intval($num);
		$site = M('Site') -> where(array('id' => __SITEID__)) -> find();
		$createsite = M('Site') -> where(array('id' => $siteid)) -> find();
		$levelprice = $this -> where(array('siteid' => $siteid, 'agentlevel_id' => $site['agentlevel_id'])) -> find();
		$level = M('Agentlevel') -> where(array('id' => $site['agentlevel_id'])) -> find();
		$discount = $level['discount'];
		if($levelprice){
			$this->discount = 100;
		}else{
			$this->discount = $discount;
		}
		$ret = $levelprice ? $this->getprice($levelprice['price'], $num) : round($this->getprice($createsite['price'], $num) * $discount / 100, 2);
		
		return $ret;
	}
	
	
	//获取一个商品的代理续费价格
	public function getagentrenewpricebylevel($siteid, $num){
		$num = intval($num);
		$site = M('Site') -> where(array('id' => __SITEID__)) -> find();
		$createsite = M('Site') -> where(array('id' => $siteid)) -> find();
		$levelprice = M('Agentrenewprice') -> where(array('siteid' => $siteid, 'agentlevel_id' => $site['agentlevel_id'])) -> find();
		
		//echo M('Agentrenewprice') -> getLastSql();
		
		$level = M('Agentlevel') -> where(array('id' => $site['agentlevel_id'])) -> find();
		$discount = $level['discount'];
		if($levelprice){
			$this->discount = 100;
		}else{
			$this->discount = $discount;
		}
		//dump($levelprice);
		$ret = $levelprice ? $this->getprice($levelprice['price'], $num) : round($this->getprice($createsite['price'], $num) * $discount / 100, 2);
		
		return $ret;
	}
	
	
	//根据数量得出最终价格
	public function getprice($price, $num){//dump($price);
		$dl = json_decode($price, true);//dump($dl);
		$num = $num == 0 ? 1 : $num;
		foreach($dl as $v){
			if($num >= $v['num']){
				$price = $v['price'];
			}
		}
		return $price * $num;
	}
	
	
	
	
	//根据数量得出最终分成金额 
	public function getaff($price, $num){
		$dl = json_decode($price, true);//dump($dl);
		$num = $num == 0 ? 1 : $num;
		foreach($dl as $v){
			if($num >= $v['num']){
				$price = $v['aff'];
			}
		}
		return $price;
	}
	
	
	
	//获取代理出售的价格
	public function getsaleprice($siteid){
		$site = M('Site') -> where(array('id' => __SITEID__)) -> find();
		$createsite = M('Site') -> where(array('id' => $siteid)) -> find();
		$levelprice = $this -> where(array('siteid' => $siteid, 'agentlevel_id' => $site['agentlevel_id'])) -> find();
		$level = M('Agentlevel') -> where(array('id' => $site['agentlevel_id'])) -> find();
		$discount = $level['discount'];
		if($levelprice){
			$this->discount = 100;
			//如果用户数量不等0，则按照 月数*单价*积分
			
		}else{
			$this->discount = $discount;
		}
		$ret = $levelprice ? $levelprice['price'] : $createsite['price'];
		return $ret;
	}
	
	
	
	//获取代理出售的价格
	public function getsalerenewprice($siteid){
		$site = M('Site') -> where(array('id' => __SITEID__)) -> find();
		$createsite = M('Site') -> where(array('id' => $siteid)) -> find();
		$levelprice = M('Agentrenewprice') -> where(array('siteid' => $siteid, 'agentlevel_id' => $site['agentlevel_id'])) -> find();
		$level = M('Agentlevel') -> where(array('id' => $site['agentlevel_id'])) -> find();
		$discount = $level['discount'];
		if($levelprice){
			$this->discount = 100;
		}else{
			$this->discount = $discount;
		}
		$ret = $levelprice ? $levelprice['price'] : $createsite['price'];
		return $ret;
	}
	
	
	
	
}
